package org.ycx.generator.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;

import java.util.List;

/**
 * 生成model中，字段增加注释
 */
public class CommentGenerator extends DefaultCommentGenerator {

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        super.addFieldComment(field, introspectedTable, introspectedColumn);
        if (introspectedColumn.getRemarks() != null && !introspectedColumn.getRemarks().equals("")) {
            field.addJavaDocLine("/** " + introspectedColumn.getRemarks() + " */");
            int type = 0;
            /**
             * 数据类型
             * 0 字符串
             * 1 数字
             * 2 浮点数
             * 3.日期
             * 4.选择框
             * 5.图片
             * 6.文件
             */
            switch (introspectedColumn.getFullyQualifiedJavaType().getShortName()) {
                case "String":
                    type = 0;
                    break;
                case "Integer":
                    type = 1;
                    break;
                case "Double":
                    type = 2;
                    break;
                case "Date":
                    type = 3;
                    break;
                default:
                    type = 0;
                    break;
            }
            field.addAnnotation("@FormDesc(name=\"" + introspectedColumn.getRemarks() + "\",maxLength=" + introspectedColumn.getLength() + ",type="+type+")");
        }
    }


    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        super.addModelClassComment(topLevelClass, introspectedTable);
        List<IntrospectedColumn> pks = introspectedTable.getPrimaryKeyColumns();
        if (pks.size() >= 2) {
            topLevelClass.addAnnotation("@FormDesc(isMidTable=true)");
        } else {
            topLevelClass.addAnnotation("@FormDesc(pk=\"" + pks.get(0).getJavaProperty() + "\")");
        }
    }
}
